package com.finace.money.ui.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.finace.money.R;
import com.finace.money.common.util.UIUtil;
import com.finace.money.data.bean.TickBean;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by zhangpeng on 2017/12/9.
 */

public class DealRightListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private List<TickBean> mTickList;
    private TickBean mTickBean;

    public void setmTickBean(TickBean mTickBean) {
        this.mTickBean = mTickBean;
    }

    public void setmTickList(List<TickBean> mTickList) {
        this.mTickList = mTickList;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new DealRightListHolder(LayoutInflater.from(UIUtil.getContext()).inflate(
                R.layout.item_deal_detail_right, parent, false
        ));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        if (holder instanceof DealRightListHolder) {
            DealRightListHolder dealRightListHolder = (DealRightListHolder) holder;
            if (null != mTickList && mTickList.size() > 0) {
                TickBean tickBean = mTickList.get(position);
                TickBean preTickBean;
                if(null != tickBean){
                    if(position == 0){
                        if(null != mTickBean){
                            preTickBean = mTickBean;
                        }else {
                            preTickBean = null;
                        }
                        dealRightListHolder.tvTickStatus.setText("-");
                    }else {
                        preTickBean = mTickList.get(position-1);
                    }
                    if(null != preTickBean){
                        double addOi = Double.valueOf(tickBean.getOi())-Double.valueOf(preTickBean.getOi());
                        double addTrade = Double.valueOf(tickBean.getTrade()) - Double.valueOf(preTickBean.getTrade());
                        double addVol = Double.valueOf(tickBean.getVol()) - Double.valueOf(preTickBean.getVol());

                        if(addOi == Double.valueOf(tickBean.getSize()) && addTrade>0){
                            dealRightListHolder.tvTickStatus.setText(R.string.duo_kai);
                            dealRightListHolder.tvTickCount.setTextColor(UIUtil.getColor(R.color.colorRedFF3B30));
                            dealRightListHolder.tvTickStatusCount.setTextColor(UIUtil.getColor(R.color.colorRedFF3B30));
                        }else if(addOi == Double.valueOf(tickBean.getSize()) && addTrade<0){
                            dealRightListHolder.tvTickStatus.setText(R.string.kong_kai);
                            dealRightListHolder.tvTickCount.setTextColor(UIUtil.getColor(R.color.colorGreen4CD964));
                            dealRightListHolder.tvTickStatusCount.setTextColor(UIUtil.getColor(R.color.colorGreen4CD964));
                        }else if(addOi == -Double.valueOf(tickBean.getSize()) && addTrade>0){
                            dealRightListHolder.tvTickStatus.setText(R.string.duo_ping);
                            dealRightListHolder.tvTickCount.setTextColor(UIUtil.getColor(R.color.colorGreen4CD964));
                            dealRightListHolder.tvTickStatusCount.setTextColor(UIUtil.getColor(R.color.colorGreen4CD964));
                        }else if(addOi == -Double.valueOf(tickBean.getSize()) && addTrade<0){
                            dealRightListHolder.tvTickStatus.setText(R.string.kong_ping);
                            dealRightListHolder.tvTickCount.setTextColor(UIUtil.getColor(R.color.colorRedFF3B30));
                            dealRightListHolder.tvTickStatusCount.setTextColor(UIUtil.getColor(R.color.colorRedFF3B30));
                        }else if(addOi == 2*Double.valueOf(tickBean.getSize()) && addVol == addOi){
                            dealRightListHolder.tvTickStatus.setText(R.string.double_kai);
                            dealRightListHolder.tvTickCount.setTextColor(UIUtil.getColor(R.color.colorWhite));
                            dealRightListHolder.tvTickStatusCount.setTextColor(UIUtil.getColor(R.color.colorWhite));
                        }else if(addOi == -2*Double.valueOf(tickBean.getSize())){
                            dealRightListHolder.tvTickStatus.setText(R.string.double_ping);
                            dealRightListHolder.tvTickCount.setTextColor(UIUtil.getColor(R.color.colorWhite));
                            dealRightListHolder.tvTickStatusCount.setTextColor(UIUtil.getColor(R.color.colorWhite));
                        }else {
                            dealRightListHolder.tvTickStatus.setText("-");
                            dealRightListHolder.tvTickCount.setTextColor(UIUtil.getColor(R.color.colorWhite));
                            dealRightListHolder.tvTickStatusCount.setTextColor(UIUtil.getColor(R.color.colorWhite));
                        }
                        if(Double.valueOf(tickBean.getTrade()) > Double.valueOf(tickBean.getPresettle())){
                            dealRightListHolder.tvTickTrade.setTextColor(UIUtil.getColor(R.color.colorRedFF3B30));
                        }else {
                            dealRightListHolder.tvTickTrade.setTextColor(UIUtil.getColor(R.color.colorGreen4CD964));
                        }
                        dealRightListHolder.tvTickTime.setText(tickBean.getTime());
                        dealRightListHolder.tvTickTrade.setText(tickBean.getTrade());
                        dealRightListHolder.tvTickCount.setText(tickBean.getSize());
                        dealRightListHolder.tvTickStatusCount.setText(tickBean.getSize());
                    }

                }else {
                    dealRightListHolder.tvTickTime.setText("-");
                    dealRightListHolder.tvTickTrade.setText("-");
                    dealRightListHolder.tvTickCount.setText("-");
                    dealRightListHolder.tvTickStatus.setText("-");
                    dealRightListHolder.tvTickStatusCount.setText("-");
                }

            }
        }
    }

    @Override
    public int getItemCount() {
        return null != mTickList && mTickList.size()>0?mTickList.size():0;
    }

    public class DealRightListHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_tick_time)
        TextView tvTickTime;
        @BindView(R.id.tv_tick_trade)
        TextView tvTickTrade;
        @BindView(R.id.tv_tick_count)
        TextView tvTickCount;
        @BindView(R.id.tv_tick_status)
        TextView tvTickStatus;
        @BindView(R.id.tv_tick_status_count)
        TextView tvTickStatusCount;

        public DealRightListHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
